<?php
class LeaveModel extends Model{

	private $type_id="`type_id`";
	private $type_list_cows="`type_id`, `type_name` , `type_prove`";
	private $level_id="`leave_id`";
	private $level_list_cows="a.`leave_id`, a.`leave_startTime`, a.`leave_endTime`, a.`leave_state`,a.leave_uid,b.`audience_name` as `leave_name`,b.`audience_portrait`,c.`type_id`,c.`type_name`,d.`department_name` as leave_department,d.`department_id`";
	private $leave_details_cows="a.`leave_id`,a.`leave_uid`,b.`audience_name` as leave_name,a.`leave_startTime`,a.`leave_endTime`,a.`leave_reason`,a.`leave_state`,e.`leader_id`,d.audience_name as `leader_name`,c.`type_name`,e.department_name as leave_department,f.department_name as leader_department";
	private $leave_log_cows="`log_id`, `uid`,`audience_name`, `leave_state`, `leave_content`, a.`time_update`";
//0=>"审批中",1=>"已审批",2=>"已驳回",
//3=>"申请销假",4=>"同意销假",5=>"驳回销假",
//6=>"提交人事",7=>"人事同意",8=>'人事驳回',
//9=>"销假提交人事",10=>"销假人事同意",11=>'销假人事驳回

/**
  *获取请假列表信息
  *update author: zhanghao
  *update time  : 2014-11-17 10:57
  ********************************************/	
	public function get_leave_list($data){
		//是否是人事部
		$is_personnel=M('department_personnel','','DB_MEETING')->where('personnel_id='.$data['uid'])->select();
		
		$sql = "select ".$this->level_list_cows." from `leave` as a";
		$sql.= " join audience as b on a.leave_uid=b.audience_id";
		$sql.= ' join audience_department as d on d.department_id=b.audience_department';
		$sql.= " join leave_type as c on a.leave_type=c.type_id";
		//myself
		$sql1=$sql." where leave_uid=".$data["uid"];
		//i am leader for 
		$sql2=$sql." where d.leader_id=".$data["uid"].' and leave_state in (0,3)';
		
		//分页
		$sql_page= " order by ".$this->level_id." desc";
		$sql_page.= " limit ".(($data['page']-1)*$data['count']).",".$data['count'];
		
		switch($data['type']){
			case 1://我的请假
				$sql1=$sql1.$sql_page;
				return M("leave","",'DB_MEETING')->query($sql1);
				break;
			case 2://我的审批
				if($is_personnel){
					//i am personel
					foreach($is_personnel as $val){
						if($department!='')
							$department.=','.$val['department_id'];
						else
							$department.=$val['department_id'];
					}
					$sql3.=$sql.' where leave_state in (6,10) and d.department_id in ('.$department.')';

					$sql2=$sql2.' union '.$sql3.$sql_page;
					return M("leave","",'DB_MEETING')->query($sql2);
				}else{
					$sql2=$sql2.$sql_page;
					return M("leave","",'DB_MEETING')->query($sql2);
				}
				break;
			case 3://审批历史
				if($is_personnel){
					//i am leader
					$sql2=$sql." where d.leader_id=".$data["uid"].' and leave_state in (1,2,4,5,6,7,8,9,10,11)';
					//i am personel
					foreach($is_personnel as $val){
						if($department!='')
							$department.=','.$val['department_id'];
						else
							$department.=$val['department_id'];
					}
					$sql3.=$sql.' where leave_state in (7,8,3,4,5,10,11) and d.department_id in ('.$department.')';
					$sql2=$sql2.' union '.$sql3.$sql_page;
					return M("leave","",'DB_MEETING')->query($sql2);
				}else{
					//i am leader
					$sql2=$sql." where d.leader_id=".$data["uid"].' and leave_state in (1,2,4,5,6,7,8,9,10,11)';
					$sql2=$sql2.$sql_page;
					return M("leave","",'DB_MEETING')->query($sql2);
				}
				break;
		}
	}
	
	public function get_type_list(){
		return M("leave_type","",'DB_MEETING')->select();	
	}
	
	public function create_leave($data){
		$rs= M('leave','','DB_MEETING')->add($data);
		//create leave,push for leader
		$data1['cid']   	= $data['cid'];
		$data1['app_for']	= 'leave';
		$data1['uid']   	= D('Audience')->get_leader_id($data['leave_uid']);
		$data1['title'] 	= '请假';
		$data1['content']	= '您有一个请假需要审批!';
		$data1['sound']		= $rs.',0';
		$re=A('Home://Company/Push')->push_one($data1);
		return $rs;
	}
	
	
	public function get_leave_by_id($id){
		$sql = "select ".$this->leave_details_cows." from `leave` as a";
		$sql.= " join audience as b on a.leave_uid=b.audience_id";
		$sql.= " join audience as d on b.leader_id=d.audience_id";
		$sql.= " join leave_type as c on a.leave_type=c.type_id";
		$sql.= " join audience_department as e on b.audience_department=e.department_id";
		$sql.= " join audience_department as f on d.audience_department=f.department_id";
		$sql.= " where a.leave_id=".$id;
		$sql.= " limit 1";
		$rs=M("leave","",'DB_MEETING')->query($sql);
		if($rs){
			if($rs[0]['leave_state']>2){
				$rs[0]['back_time']=M('leave_log','','DB_MEETING')->where('leave_id='.$rs[0]['leave_id'].' and leave_state=3')->getField('back_time');
			}
			$rs[0]['back_time']=$rs[0]['back_time']==null?0:$rs[0]['back_time'];
			$interval=(($rs[0]['leave_endTime']-$rs[0]['leave_startTime'])/3600)%24;
			if($interval==0){
				$rs[0]['leave_interval']=((($rs[0]['leave_endTime']-$rs[0]['leave_startTime'])/3600)/24).'天';
			}else{
				$rs[0]['leave_interval']='共'.floor((($rs[0]['leave_endTime']-$rs[0]['leave_startTime'])/3600)/24).'天'.$interval.'小时';
			}
			return $rs[0];
		}else
			return false;
	}
	
	public function update_state($data){
		
		$pass_data=$this->get_leave_by_id($data['leave_id']);
		if($pass_data['leave_state']==0 && $data['leave_state']==1){
			//同意请假
			$data1['uid']   		= $pass_data['leave_uid'];
			$data1['title'] 		= '请假';
			$data1['content']		= '领导允许请假!';
		}else if($pass_data['leave_state']==0 && $data['leave_state']==2){
			//驳回
			$data1['uid']   		= $pass_data['leave_uid'];
			$data1['title'] 		= '请假';
			$data1['content']		= '领导允许请假!';
		}else if($pass_data['leave_state']==1 && $data['leave_state']==3){
			//销假
			$data1['uid']   		= $pass_data['leader_id'];
			$data1['title'] 		= '销假';
			$data1['content']		= '新的销假需要审核!';	
		}else if($pass_data['leave_state']==3 && $data['leave_state']==4){
			//驳回
			$data1['uid']   		= $pass_data['leave_uid'];
			$data1['title'] 		= '销假';
			$data1['content']		= '销假被领导驳回!';	
		}else if($pass_data['leave_state']==3 && $data['leave_state']==5){
			//销假同意
			$data1['uid']   		= $pass_data['leave_uid'];
			$data1['title'] 		= '销假';
			$data1['content']		= '领导已同意销假!';	
		}else if($pass_data['leave_state']==0 && $data['leave_state']==6){
			//提交人事
			$data1['uid']   		= D('Department')->get_department_personel($pass_data['leave_uid']);
			$data1['title'] 		= '请假';
			$data1['content']		= '新的请假需要审核!';
		}else if($pass_data['leave_state']==6 && $data['leave_state']==7){
			//人事同意
			$data1['uid']   		= $pass_data['leave_uid'];
			$data1['title'] 		= '请假';
			$data1['content']		= '人事已同意!';	
		}else if($pass_data['leave_state']==6 && $data['leave_state']==8){
			//人事驳回
			$data1['uid']   		= $pass_data['leave_uid'];
			$data1['title'] 		= '请假';
			$data1['content']		= '请假被人事驳回!';	
		}else if($pass_data['leave_state']==3 && $data['leave_state']==9){
			//销假提交人事
			$data1['uid']   		= (int)D('Department')->get_department_personel($pass_data['leave_uid']);
			$data1['title'] 		= '请假';
			$data1['content']		= '新的请假需要审核!';	
		}else if($pass_data['leave_state']==9 && $data['leave_state']==10){
			//人事同意销假
			$data1['uid']   		= $pass_data['leave_uid'];
			$data1['title'] 		= '请假';
			$data1['content']		= '人事已同意!';	
		}else if($pass_data['leave_state']==9 && $data['leave_state']==11){
			//人事驳回销假
			$data1['uid']   		= $pass_data['leave_uid'];
			$data1['title'] 		= '请假';
			$data1['content']		= '请假被人事驳回!';	
		}
		
		$data1['sound']			= $data['leave_id'].','.$data['leave_state'];
		$data1['cid']   		= $data['cid'];
		$data1['app_for']		= 'leave';
		
		$re=A('Home://Company/Push')->push_one($data1);
		/*echo json_encode(
			array(
				"pass_state"=>$pass_data['leave_state'],
				"update_state"=>$data['leave_state'],
				"push_state1"=>$re
			)
		);
		exit();*/
		
		M('leave','','DB_MEETING')->startTrans();//在第一个模型里启用就可以了，或者第二个也行
			$rs1=M('leave_log','','DB_MEETING')->where('leave_id='.$data['leave_id'])->add($data);
			$rs2=M('leave','','DB_MEETING')->where('leave_id='.$data['leave_id'])->save($data);
		if($rs1 && $rs2){
			M('leave','','DB_MEETING')->commit();
			return $rs2;
		}else{
			M('leave','','DB_MEETING')->rollback();//不成功，则回滚
		}
	}
	
	public function get_logs($id){
		$sql = "select ".$this->leave_log_cows.",department_name from leave_log as a";
		$sql.= " join audience as b on a.uid=b.audience_id";
		$sql.= ' join audience_department c on b.audience_department=c.department_id';
		$sql.= " where leave_id=".$id;
		return M('leave_log','','DB_MEETING')->query($sql);
	}
	
	public function get_prove($id){
		return M('leave_prove','','DB_MEETING')->where('leave_id='.$id)->getField('prove_image_path');	
	}
	
	
	// +---------------------------------------+
	// | 获取任务详情                      	   |
	// +---------------------------------------+
	// | last update author: zhanghao		   |
	// | last update time  : 2014-10-28 03:22  |
	// +---------------------------------------+
	// | use this function : Leave/search_leave|
	// +---------------------------------------+
	public function search_leave($data){
		switch($data['searchType']){
			case 1:$cow='b.`audience_name`';break;
			case 2:$cow='d.`department_name`';break;
			case 3:$cow='c.`type_name`';break;
			default:
				$cow='b.`audience_name`';break;
		}
		if($data['conditions']!=''){
			$where = '  and INSTR ('.$cow.',"'.$data['conditions'].'")';
		}else{
			$where='';	
		}
		if($data['searchType']==4){
			$startTime=strtotime($data['conditions'].' 00:00:00');
			$endTime  =strtotime($data['conditions'].' 23:59:59');
			$where=' and leave_startTime>='.$startTime.' and leave_startTime<='.$endTime;
		}else if($data['searchType']==5){
			$date=explode('-',$data['conditions']);
			$startTime = strtotime($date[0].'-'.$date[1].'-'.'01 00:00:00');
			if($date[1]==12){
				$date[0]=(int)$date[0]+1;
				$date[1]='01';
			}else{
				$date[1]=(int)$date[1]+1;
			}
			$endTime = strtotime($date[0].'-'.$date[1].'-'.'01 00:00:00');
			$where=' and leave_startTime>='.$startTime.' and leave_endTime<'.$endTime;
		}
		$sql = "select ".$this->level_list_cows." from `leave` as a";
		$sql.= " join audience as b on a.leave_uid=b.audience_id";
		$sql.= ' join audience_department as d on d.department_id=b.audience_department';
		$sql.= " join leave_type as c on a.leave_type=c.type_id";
		$sql.= " where leave_uid=".$data["uid"].$where;
		$sql.= " union ";
		$sql.= " select ".$this->level_list_cows." from `leave` as a";
		$sql.= " join audience as b on a.leave_uid=b.audience_id";
		$sql.= ' join audience_department as d on d.department_id=b.audience_department';
		$sql.= " join leave_type as c on a.leave_type=c.type_id";
		$sql.= " where d.leader_id=".$data["uid"].$where;
		
		$sql.= " order by ".$this->level_id." desc";
		$sql.= " limit ".(($data['page']-1)*$data['count']).",".$data['count'];
		$rs=M("leave","",'DB_MEETING')->query($sql);
		return $rs;
	}
	
}
?>